﻿Imports System.Data.SqlClient

Public Class frmCategory

    Private Sub frmCategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            DataGrid.FormatDataGrid(grddata)
            Me.bindData("")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
    Public Sub bindData(ByVal search As String)
        Try
            If (search = "") Then
                grddata.DataSource = dbconnection.ExecuteDatatable("select a.CategoryId, a.CategoryName, a.TypeId, b.TypeName " & _
                " from Category a inner join Type b on a.TypeId=b.TypeId where a.Active=1")
            ElseIf (search <> "") Then
                grddata.DataSource = dbconnection.ExecuteDatatable("select a.CategoryId, a.CategoryName, a.TypeId, b.TypeName " & _
                " from Category a inner join Type b on a.TypeId=b.TypeId " & _
                " where a.Active=1 and UPPER(CategoryName) LIKE UPPER(N'%" & search & "%');")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Try
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
        End Try
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Try
            For Each c As Control In frmMain.pnBody.Controls
                c.Hide()
            Next
            With frmCategoryAct
                .bindtypetem()
                .lblTitle.Text = "Add Product Category"
                .lblCategoryId.Text = ""
                .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                .TopLevel = False
                .Dock = DockStyle.Fill
                frmMain.pnBody.Controls.Add(frmCategoryAct)
                .Show()
            End With
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
        End Try
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Try
            If grddata.RowCount <= 0 Then
                Exit Sub
            End If
            For Each c As Control In frmMain.pnBody.Controls
                c.Hide()
            Next
            Dim ed = frmCategoryAct
            ed.bindtypetem()
            With grddata.CurrentRow
                ed.lblCategoryId.Text = .Cells("CategoryId").Value
                ed.txtCategory.Text = .Cells("CategoryName").Value
                ed.cbtypeid.SelectedValue = .Cells("TypeId").Value
            End With
            With ed
                .lblTitle.Text = "Edit Product Category"
                .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                .TopLevel = False
                .Dock = DockStyle.Fill
                frmMain.pnBody.Controls.Add(frmCategoryAct)
                .Show()
            End With
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
        End Try
    End Sub

    Private Shared Function validpro(ByVal cateid As String)
        Try
            Dim prohave = dbconnection.ExecuteScalar("select * from Product where CategoryId='" + cateid + "';")
            Return prohave
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
            Return False
        End Try
    End Function

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            If grddata.RowCount <= 0 Then
                Exit Sub
            End If
            If (validpro(grddata.CurrentRow.Cells("CategoryId").Value) IsNot Nothing) Then
                MessageBox.Show("Errors: this type still active in product, please go to remove it first...!")
            Else
                If MessageBox.Show("Do you want to delete this record?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    Dim cmddel As SqlCommand
                    cmddel = dbconnection.Connection.CreateCommand
                    With cmddel
                        .CommandType = CommandType.Text
                        .CommandText = "update Category set Active=0 where CategoryId=@CategoryId;"
                        .Parameters.Add("@CategoryId", SqlDbType.NVarChar).Value = grddata.CurrentRow.Cells("CategoryId").Value
                        .ExecuteNonQuery()
                    End With
                    MessageBox.Show("Data Deleted")
                    Me.bindData("")
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
        End Try
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        Me.bindData(txtsearch.Text.Trim)
    End Sub
End Class